package com.wiseMen.weixin.main;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.sword.wechat4j.WechatSupport;
import org.sword.wechat4j.event.EventType;
import org.sword.wechat4j.message.CustomerMsg;
import org.sword.wechat4j.request.WechatRequest;
import org.sword.wechat4j.response.ArticleResponse;

import com.wiseMen.weixin.utils.SessionUtils;

public class WechatAction extends WechatSupport {

	private WeChatService service;
	
	public WechatAction(HttpServletRequest request) {
		super(request);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		this.service = (WeChatService)wac.getBean("WeChatService");
	}

	private static String normalText = "你好，请通过微信底部功能菜单与我们交互！";
	
	/**
	 * 文本消息
	 */
	@Override
	protected void onText() {
		try{
		}catch(Exception ex){
		}
		responseText(normalText);
	}
	/**
	 * 图片消息
	 */
	@Override
	protected void onImage() {
		responseText(normalText);
	}

	/**
	 * 语音消息
	 */
	@Override
	protected void onVoice() {
		responseText(normalText);
	}

	/**
	 * 视频消息
	 */
	@Override
	protected void onVideo() {
		responseText(normalText);
	}
	
	/**
	 * 地理位置消息
	 */
	@Override
	protected void onLocation() {
		responseText(normalText);
	}
	/**
	 * 链接消息
	 */
	@Override
	protected void onLink() {
		responseText(normalText);
	}
	
	
	/**
	 * 未知消息类型，错误处理
	 */
	@Override
	protected void onUnknown() {
		responseText(normalText);
	}

	/**
	 * 扫描二维码事件
	 */
	@Override
	protected void scan() {
		responseText(normalText);
	}

	/**
	 * 关注事件
	 */
	@Override
	protected void subscribe() {
		try{
			this.service.subscribe(wechatRequest.getFromUserName(), 1);
			
			responseText("你好，欢迎关注河源市中小企业综合服务平台！");
			
			CustomerMsg c = new CustomerMsg(wechatRequest.getFromUserName());
			
			ArticleResponse r = new ArticleResponse();
			r.setDescription("用足、用活、用好“营改增”政策，提升企业经营管理能力");
			r.setPicUrl("http://sme.heyuan.gov.cn/u/cms/www/201607/26171604r3c1.JPG");
			r.setTitle("用足、用活、用好“营改增”政策，提升企业经营管理能力");
			r.setUrl("http://wxapi.heyuanzhfw.org/admin/weixin/subscribeShow.do?index=1");
			
			ArticleResponse r1 = new ArticleResponse();
			r1.setDescription("举办河源市中小企业政策宣传活动");
			r1.setPicUrl("http://sme.heyuan.gov.cn/u/cms/www/201509/221643505wyv.png");
			r1.setTitle("举办河源市中小企业政策宣传活动");
			r1.setUrl("http://wxapi.heyuanzhfw.org/admin/weixin/subscribeShow.do?index=2");
			
			ArticleResponse r2 = new ArticleResponse();
			r2.setDescription("广东省中小企业技术服务联盟成立大会顺利召开");
			r2.setPicUrl("http://www.gdsme.com.cn/temp/ebff2176-64c1-4660-a353-0f2796be0f4d.jpg");
			r2.setTitle("广东省中小企业技术服务联盟成立大会顺利召开");
			r2.setUrl("http://wxapi.heyuanzhfw.org/admin/weixin/subscribeShow.do?index=3");
			
			List<ArticleResponse> list = new ArrayList<ArticleResponse>();
			list.add(r);
			list.add(r1);
			list.add(r2);
			c.sendNews(list);
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	/**
	 * 取消关注事件
	 */
	@Override
	protected void unSubscribe() {
		try{
			this.service.subscribe(wechatRequest.getFromUserName(), 2);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	/**
	 * 点击菜单跳转链接时的事件推送
	 */
	@Override
	protected void view() {
		SessionUtils.setOpenId(wechatRequest.getFromUserName());
	}

	/**
	 * 自定义菜单事件
	 */
	@Override
	protected void click() {
	}
	
	/**
	 * 上报地理位置事件
	 */
	@Override
	protected void location() {
		responseText(normalText);
	}
	
	/**
	 * 模板消息发送成功推送事件
	 */
	@Override
	protected void templateMsgCallback() {
		String MsgID = wechatRequest.getMsgId();
		String Status = wechatRequest.getStatus();
		String result = "模板消息发送成功推送事件MsgID:" + MsgID + ", Status:" + Status;
		responseText(result);
	}
	/**
	 * 弹出地理位置选择器的事件
	 */
	@Override
	protected void locationSelect() {
		String Location_X = wechatRequest.getSendLocationInfo().getLocation_X();
		String Location_Y = wechatRequest.getSendLocationInfo().getLocation_Y();
		String Scale = wechatRequest.getSendLocationInfo().getScale();
		String Label = wechatRequest.getSendLocationInfo().getLabel();
		String Poiname = wechatRequest.getSendLocationInfo().getPoiname();
		String result = "弹出地理位置选择器的事件Location_X:" + Location_X + 
				", Location_Y:" + Location_Y+ 
				", Scale:" + Scale+ 
				", Label:" + Label+ 
				", Poiname:" + Poiname;
		responseText(result);
	}
	/**
	 * 弹出拍照或者相册发图的事件
	 */
	@Override
	protected void picPhotoOrAlbum() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String PicMd5Sum = "";
		if(StringUtils.isNotBlank(Count) && !Count.equals("0")){
			PicMd5Sum = wechatRequest.getSendPicsInfo().getItem().get(0).getPicMd5Sum();
		}
		String result = "弹出系统拍照发图的事件Count:" + Count + ", PicMd5Sum:" + PicMd5Sum;
		responseText(result);
	}
	/**
	 * 弹出系统拍照发图的事件
	 */
	@Override
	protected void picSysPhoto() {
		String Count = wechatRequest.getSendPicsInfo().getCount();
		String result = "弹出系统拍照发图的事件Count:" + Count ;
		responseText(result);
	}
	/**
	 * 弹出微信相册发图器的事件推送
	 */
	@Override
	protected void picWeixin() {
	}
	/**
	 * 扫码推事件
	 * 
	 */
	@Override
	protected void scanCodePush() {
		String ScanType = wechatRequest.getScanCodeInfo().getScanType();
		String ScanResult = wechatRequest.getScanCodeInfo().getScanResult();
		String result = "扫码推事件ScanType:" + ScanType + ", ScanResult:" + ScanResult;
		responseText(result);
	}
	/**
	 * 扫码推事件且弹出“消息接收中”提示框的事件
	 */
	@Override
	protected void scanCodeWaitMsg() {
		String ScanType = wechatRequest.getScanCodeInfo().getScanType();
		String ScanResult = wechatRequest.getScanCodeInfo().getScanResult();
		String result = "扫码推事件ScanType:" + ScanType + ", ScanResult:" + ScanResult;
		responseText(result);
	}
	
	/**
	 * 保存点击按钮记录
	 * @param we
	 */
	private void saveEventRecord(WechatRequest we){
		String openId = we.getFromUserName();
		String eventType = EventType.valueOf(we.getEvent()).name();
		String eventKey = we.getEventKey();
		try{
		}catch(Exception ex){
		}
	}

	@Override
	protected void kfCloseSession() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void kfCreateSession() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void kfSwitchSession() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onShortVideo() {
		// TODO Auto-generated method stub
		
	}

}
